/**
 * Javascript library for template ExtremeMagento
 * @copyright 2007 Quick Solution LTD. All rights reserved.
 * @author Giao L. Trinh <giao.trinh@quicksolutiongroup.com>
 */

(function() {
	
// EM.tools {{{
	
if (typeof BLANK_IMG == 'undefined') 
	var BLANK_IMG = '';

// declare namespace() method
String.prototype.namespace = function(separator) {
  this.split(separator || '.').inject(window, function(parent, child) {
    var o = parent[child] = { }; return o;
  });
};


'EM.tools'.namespace();

function menu()
{
	var Width_ul=960;
	var Width_li=0;
	var Width_before=0;
	var Width_div=0;
	var Width=0;
	
	$$("#nav").each(function(elem) {
	elem.childElements().each(function(li) {
		li.addClassName('submenu');
	});
		
	});
	
	
    $$(".menu").each(function(elem) {
		elem.childElements().each(function(li) {
			li.addClassName('submenu');
            Width_li=li.getWidth();
			Width=Width_ul-Width_before;
			Width_before+=Width_li;
			$div=li.select('div')[0];
			if(typeof $div != 'undefined'){
				Width_div=$div.getWidth();
				sub=Width_div-Width;
				if(sub>0){
					$div.addClassName(' position-right')
					li.addClassName('position-right-li')
				}
			}
        });
		
	});
}

function scrollbar1() {
	var $$li = $$('#scrollbar1 ul.products-grid li.item');
	if ($$li.length > 0) {
		
		// reset UL's width
		var ul = $$('#scrollbar1 ul.products-grid')[0];
		var w = 0;
		$$li.each(function(li) {
			w += li.getWidth();
		});
		ul.setStyle({'width':w+'px'});		
	}
}

function initCarousel($select,$scroll,$auto,$vertical){
	if(jQuery($select).length > 0)
		jQuery($select).jcarousel({
			wrap: 'circular',
			buttonNextHTML:'<a class="next" href="javascript:void(0);">Next</a>',
			buttonPrevHTML:'<a class="previous" href="javascript:void(0);">Pre</a>',
			scroll:$scroll,
			animation:1000,
			auto:$auto,
			vertical:$vertical
		});
		
}

function initToggleTabs($selector){
	if(jQuery($selector).length > 0){
		var timeout = new Array(jQuery($selector).length);
		var div = new Array(jQuery($selector).length);
		jQuery($selector).addClass('ui-tabs ui-widget ui-widget-content ui-corner-all');
		jQuery($selector).each(function(index,value){
			timeout[index] = null;
			div[index] = jQuery(this);
			div[index].addClass('ui-tabs-nav ui-helper-reset ui-helper-clearfix ui-widget-header ui-corner-all');
			//When page loads...
			div[index].find(".tab-item").hide(); //Hide all content
			div[index].children('ul').find("li:first").addClass("ui-tabs-selected").show(); //Activate first tab
			div[index].children('ul').addClass('ui-tabs-nav ui-helper-reset ui-helper-clearfix ui-widget-header ui-corner-all');
			div[index].children('ul').find('li').addClass('ui-state-default ui-corner-top');	
			div[index].find(".tab-item:first").show(); //Show first tab content
			//On Click Event
			div[index].children('ul').find("li").click(function() {
				var currentTab = jQuery(this);
				if(currentTab.hasClass('ui-tabs-selected'))
					return false;
				if (timeout[index])
					clearTimeout(timeout[index]);
				timeout[index] = setTimeout(function() {
					timeout[index] = null;	
					// Hide old content tab
					jQuery(div[index].children('ul').find('li.ui-tabs-selected a').attr('href')).toggle('slow');
					
					div[index].children('ul').find("li").removeClass("ui-tabs-selected"); //Remove any "ui-tabs-selected" class
					currentTab.addClass("ui-tabs-selected"); //Add "active" class to selected tab
					
					var activeTab = currentTab.find("a").attr("href"); //Find the href attribute value to identify the active tab + content
					jQuery(activeTab).toggle('slow'); //Fade in the active ID content
					return false;
				}, 10);
				return false;
			});	
		});
		
	}
}

function mycarousel_initCallback(carousel) {
    jQuery('#slideshow_crosssell .navigation a').bind('click', function() {
        jQuery('#slideshow_crosssell .navigation a').removeClass('active');
		jQuery(this).addClass('active');
		carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
        return false;
    });
}

document.observe("dom:loaded", function() {
	
	menu();
	scrollbar1();
	
	initCarousel('div#slideshow_moreviews.run_slider > div > ul',1,0,false);
	
	/* use for slide-np*/
	initCarousel('#slideshow_toppart > div > ul',1,0,true);
	initCarousel('#home_tab_cagories div.run_slider > ul',4,0,false);
	
	/* Product Detail Page */
	initCarousel('.tabs_view_relate_upsell div.run_slider > ul',1,0,false);
	
	initToggleTabs('#home_tab_cagories');
	initToggleTabs('.tabs_view_relate_upsell');
	initToggleTabs('#detailtabs');
	initToggleTabs('#customer-reviews');
		

	
	if(jQuery('#slideshow_crosssell .slideshow-box').length > 0){
		jQuery("#slideshow_crosssell .slideshow-box").jcarousel({
			scroll: 4,
			initCallback: mycarousel_initCallback,
			// This tells jCarousel NOT to autobuild prev/next buttons
			buttonNextHTML: null,
			buttonPrevHTML: null,			
			animation:1000,
			wrap: 'circular'
		});
	}
	jQuery('#scrollbar1').tinyscrollbar({ axis: 'x'});
	
});

// }}}

})();